package com.rc.saas.mini.controller.rc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.mapper.rc.RcEvaluateMapper;
import com.rc.saas.mini.minip.vo.LoginInfoVo;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.order.OrderInfo;
import com.rc.saas.mini.model.rc.RcEvaluate;
import com.rc.saas.mini.service.member.MemberInfoService;
import com.rc.saas.mini.service.member.MemberInfoServiceImpl;
import com.rc.saas.mini.service.order.OrderInfoService;
import com.rc.saas.mini.service.rc.RcEvaluateServiceImpl;
import com.rc.saas.mini.vo.base.RcPageVo;
import com.rc.saas.mini.vo.order.OrderInfoVo;
import com.rc.saas.mini.vo.rc.RcEvaluateVo;
import com.rc.saas.mini.vo.rc.RcNotEvaluateVo;
import com.rc.saas.mini.vo.rc.RcWithEvaluateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author
 * @Date: 2022-05-13
 */


@Controller
@Api(tags = "10.站点会员评论")
@RequestMapping("/rcEvaluate")
public class RcEvaluateController extends MinipBaseController {

    private static Logger logger = LogManager.getLogger(RcEvaluateController.class);

    @Autowired
    private RcEvaluateServiceImpl rcEvaluateServiceImpl;

    @Autowired
    private RcEvaluateMapper rcEvaluateMapper;


    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private OrderInfoService orderInfoService;
    ///**

    /**
     * 增、改
     */
    @ResponseBody
    @RequestMapping(value = "/saveOrUpdateRcEvaluate", method = RequestMethod.POST)
    @ApiOperation(value = "01.增、改 站点会员评论表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    private DataTResponse<String> saveOrUpdateRcEvaluate(HttpServletRequest request, RcEvaluate rcEvaluate) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
        if (memberInfo == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        rcEvaluate.setMemberId(loginInfo.getMemberId());
        rcEvaluate.setHeadLogo(memberInfo.getHeadImg());
        rcEvaluate.setNickname(memberInfo.getNickname());
        if (rcEvaluate == null || rcEvaluate.getOrderId() == null) {
            return DataTResponse.FAIL("无订单id");
        }
        if (memberInfo.getMobile() != null) {
            int length = memberInfo.getMobile().length();
            memberInfo.setMobile(memberInfo.getMobile().substring(0, 3) + "******" + memberInfo.getMobile().substring(length - 2, length));
        }
        rcEvaluate.setMobile(memberInfo.getMobile());
        rcEvaluate.setStatus("0");
        rcEvaluate.setTenantCode(loginInfo.getTenantCode());
        rcEvaluate.setIsDelete("0");
        OrderInfo orderInfo = orderInfoService.selectOrderById(rcEvaluate.getOrderId());
        rcEvaluate.setElecStationId(orderInfo.getElecStationId());
        if (rcEvaluate.getEvaluateId() == null) {

            orderInfo.setEvaluate(rcEvaluate.getEvaluate());
            orderInfo.setEvaStar(rcEvaluate.getStationStar());
            orderInfo.setLastUpdatedTime(new Date());
            orderInfoService.updateByPrimaryKeySelective(orderInfo);
            rcEvaluate.setCreatedTime(new Date());

            rcEvaluate.setCreatedBy(loginInfo.getMemberId() + "");
            rcEvaluateServiceImpl.insertSelective(rcEvaluate);
        } else {
            RcEvaluate rcEvaluate1 = rcEvaluateServiceImpl.selectByPrimaryKey(rcEvaluate.getEvaluateId());
            if (!"0".equals(rcEvaluate1.getEvaluateId())) {
                return DataTResponse.FAIL("只能修改未审核的评论");
            }
            if (!rcEvaluate1.getMemberId().equals(loginInfo.getMemberId())) {
                return DataTResponse.FAIL("只能修改自己的评论");
            }

            orderInfo.setEvaluate(rcEvaluate.getEvaluate());
            orderInfo.setEvaStar(rcEvaluate.getStationStar());
            orderInfo.setLastUpdatedTime(new Date());
            orderInfoService.updateByPrimaryKeySelective(orderInfo);
            rcEvaluate.setLastUpdatedTime(new Date());
            rcEvaluate.setLastUpdatedBy(loginInfo.getMemberId() + "");
            rcEvaluateServiceImpl.updateByPrimaryKeySelective(rcEvaluate);
        }
        return DataTResponse.SUCCESS();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "/deleteRvEvaluate", method = RequestMethod.POST)
    @ApiOperation(value = "删除评论表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    private DataTResponse<String> deleteRvEvaluate(HttpServletRequest request, Long evaluateId) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(loginInfo.getMemberId());
        if (memberInfo == null) {
            return DataTResponse.FAIL_LOGIN();
        }
        RcEvaluate rcEvaluate1 = rcEvaluateServiceImpl.selectByPrimaryKey(evaluateId);
        if ("2".equals(rcEvaluate1.getStatus())) {
            return DataTResponse.FAIL("不能修改审核通过的评论");
        }
        if (!rcEvaluate1.getMemberId().equals(loginInfo.getMemberId())) {
            return DataTResponse.FAIL("只能修改自己的评论");
        }

        OrderInfo orderInfo = orderInfoService.selectOrderById(rcEvaluate1.getOrderId());
        if (orderInfo == null) {
            return DataTResponse.FAIL("只能修改自己的评论");
        }
        orderInfo.setEvaluate(null);
        orderInfo.setEvaStar(null);
        orderInfo.setLastUpdatedTime(new Date());
        orderInfoService.updateByPrimaryKeySelective(orderInfo);
        rcEvaluate1.setIsDelete("1");
        rcEvaluate1.setLastUpdatedTime(new Date());
        rcEvaluate1.setLastUpdatedBy(loginInfo.getMemberId() + "");
        rcEvaluateServiceImpl.deleteByPrimaryKey(rcEvaluate1.getEvaluateId());

        return DataTResponse.SUCCESS();
    }


    /**
     * 查询列表分页
     */
    @RequestMapping(value = "/listPageRcEvaluateByElecStationId", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "03.查询列表分页 站点评论表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "elecStationId", paramType = "query", value = "站点id", required = true),
    })
    private DataTResponse<PageInfo<RcEvaluate>> listPageRcEvaluateByElecStationId(HttpServletRequest request, RcEvaluate rcEvaluate, RcPageVo rcPageVo) {
        //Page
        //RcEvaluate rcEvaluate=new RcEvaluate();
        //rcEvaluate.setElecStationId(elecStaionId);
        rcEvaluate.setStatus("2");
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());
        List<RcEvaluate> list = rcEvaluateMapper.listByCondition(rcEvaluate);
        PageInfo<RcEvaluate> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(pageInfo);
    }

    //
    ///**
    // * 查询列表分页
    // */
    //
    //@RequestMapping(value = "/listPageByRcEvaluate", method = RequestMethod.POST)
    //@ResponseBody
    //@ApiOperation(value = "04.查询列表分页 站点评论（自定义查询条件,尽量不用）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //private DataTResponse<PageInfo<RcEvaluate>> listPageByRcEvaluate(HttpServletRequest request, RcEvaluate rcEvaluate, RcPageVo rcPageVo) {
    //    //Page
    //    //RcEvaluate rcEvaluate=new RcEvaluate();
    //    //rcEvaluate.setElecStationId(elecStaionId);
    //    //rcEvaluate.setStatus("2");
    //    PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());
    //    List<RcEvaluate> list = rcEvaluateMapper.listByCondition(rcEvaluate);
    //    PageInfo<RcEvaluate> pageInfo = new PageInfo<>(list);
    //    return DataTResponse.SUCCESS_DATA(pageInfo);
    //}

    /**
     * 未评价订单查询查询列表分页
     */
    @RequestMapping(value = "/listPageRcNotEvaluateVo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "待评价，订单查询查询列表分页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    private DataTResponse<PageInfo<RcNotEvaluateVo>> listPageRcNotEvaluateVo(HttpServletRequest request, RcPageVo rcPageVo) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());
        List<OrderInfoVo> list = orderInfoService.listPageByRcEvaluateWithOrder(loginInfo.getMemberId());
        PageInfo<OrderInfoVo> pageInfo = new PageInfo<>(list);
        PageInfo<RcNotEvaluateVo> pageInfo1 = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, pageInfo1);
        List<RcNotEvaluateVo> rcNotEvaluateVoList = new ArrayList<>();
        for (OrderInfoVo orderInfoVo : pageInfo.getList()) {
            RcNotEvaluateVo rcNotEvaluateVo = new RcNotEvaluateVo();
            BeanUtils.copyProperties(orderInfoVo, rcNotEvaluateVo);
            rcNotEvaluateVoList.add(rcNotEvaluateVo);
        }
        List<RcNotEvaluateVo> rcWithEvaluateVoListDesc =rcNotEvaluateVoList.stream().sorted(Comparator.comparing(RcNotEvaluateVo::getPayTime).reversed()).collect(Collectors.toList());
        pageInfo1.setList(rcWithEvaluateVoListDesc);
        return DataTResponse.SUCCESS_DATA(pageInfo1);
    }


    /**
     * 查询列表分页
     */
    @ResponseBody
    @RequestMapping(value = "/listPageByRcWithEvaluateVo", method = RequestMethod.POST)
    @ApiOperation(value = "已评价，查询列表分页 自己评论表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    private DataTResponse<PageInfo<RcWithEvaluateVo>> listPageByRcWithEvaluateVo(HttpServletRequest request, RcPageVo rcPageVo) {
        //Page
        LoginInfoVo loginInfo = readLoginInfo(request);
        //RcEvaluate rcEvaluate=new RcEvaluate();
        PageHelper.startPage(rcPageVo.getPageNo(), rcPageVo.getPageSize());
        List<OrderInfoVo> list = orderInfoService.listByRcWithEvaluateVo(loginInfo.getMemberId());
        List<RcEvaluate> rcEvaluateList = rcEvaluateServiceImpl.listByMemberId(loginInfo.getMemberId());
        PageInfo<OrderInfoVo> pageInfo = new PageInfo<>(list);
        PageInfo<RcWithEvaluateVo> pageInfo1 = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, pageInfo1);
        List<RcWithEvaluateVo> rcWithEvaluateVoList = new ArrayList<>();
        for (OrderInfoVo orderInfoVo : pageInfo.getList()) {
            RcWithEvaluateVo rcWithEvaluateVo = new RcWithEvaluateVo();
            BeanUtils.copyProperties(orderInfoVo, rcWithEvaluateVo);

            RcEvaluate rcEvaluate = rcEvaluateList.stream().filter(data -> data.getOrderId().equals(orderInfoVo.getOrderId())).findFirst().orElseGet(() -> null);
            if (rcEvaluate != null) {
                rcWithEvaluateVo.setEvaluateId(rcEvaluate.getEvaluateId());
                rcWithEvaluateVo.setEvaStar(rcEvaluate.getStationStar());
            }

            rcWithEvaluateVoList.add(rcWithEvaluateVo);
        }
        List<RcWithEvaluateVo> rcWithEvaluateVoListDesc =rcWithEvaluateVoList.stream().sorted(Comparator.comparing(RcWithEvaluateVo::getEvaluateTime).reversed()).collect(Collectors.toList());
        pageInfo1.setList(rcWithEvaluateVoListDesc);
        return DataTResponse.SUCCESS_DATA(pageInfo1);
    }

}
